package cn.gdcp.lkqapp02;

import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {
    private WebView my_WebView;
    private String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        my_WebView = findViewById(R.id.my_webview);
        /*my_WebView.loadUrl("http://www.gdcp.cn");*/
        /*my_WebView.loadUrl("http://www.baidu.com");*/
        /*my_WebView.loadUrl("file:///android_asset/test.html");*/
        String htmlContent = "<html>\n" + "\n" +
                "<body>\n" +
                "HelloWorld!\n" + "<br>" +
                "</body>\n" + "<img src=\"file:///android_asset/img1.gif\" width=\"100\" height=\"100\" >\n " +
                "</html>";

        my_WebView.loadDataWithBaseURL(null, htmlContent, "text/html", "utf-8", null);

        my_WebView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Log.e(TAG,"url = " + url);
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                Log.e(TAG,"开始加载页面");
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                Log.e(TAG,"页面加载完成");
                super.onPageFinished(view, url);
            }
        });

        WebSettings webSetting = my_WebView.getSettings();
        webSetting.setJavaScriptEnabled(true);
        webSetting.setSupportZoom(true);
        webSetting.setBuiltInZoomControls(true);
        webSetting.setDisplayZoomControls(true);

    }

    @Override
    public void onBackPressed() {
        if (my_WebView.canGoBack()){
            my_WebView.goBack();
        }else {
            super.onBackPressed();
        }
    }


}
